#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==================================================
# @File    : request
# @IDE     : PyCharm
# @Author  : liuxiangui
# @Date    : 2023/12/29 22:32
# @Describe: 
# ==================================================

import requests
from requests import Response
import urllib3
from typing import Any, Text, Dict, Union
from library.log import logger


class RequestHttp:
    def __init__(self):
        urllib3.disable_warnings()
        self.request_session = requests.Session()

    def add_request_headers(self, headers: Dict):
        self.request_session.headers.update(headers)

    def send(self,
             method: Text,
             url: Text,
             data: Any = None,
             is_json: bool = True,
             timeout: int = 6,
             proxies: Union[Dict, None] = None,
             verify: bool = False,
             files: Union[Text, None] = None,
             **kwargs
             ) -> Response:
        if isinstance(type(data), str):
            data = eval(data)
        method = method.lower()
        if method == "get":
            try:
                result = self.request_session.request(method=method,
                                                      url=url,
                                                      params=data,
                                                      timeout=timeout,
                                                      proxies=proxies,
                                                      verify=verify,
                                                      files=files,
                                                      **kwargs)
            except Exception as e:
                logger.error(f"request failed! cause by: {e}")
                raise
        elif method in ("post", "delete", "put", "head", "options"):
            if is_json:
                # 传递json
                try:
                    result = self.request_session.request(method=method,
                                                          url=url,
                                                          json=data,
                                                          timeout=timeout,
                                                          proxies=proxies,
                                                          verify=verify,
                                                          files=files,
                                                          **kwargs)
                except Exception as e:
                    logger.error(f"request failed! cause by: {e}")
                    raise
            else:
                # 传递表单
                try:
                    result = self.request_session.request(method=method,
                                                          url=url,
                                                          data=data,
                                                          timeout=timeout,
                                                          proxies=proxies,
                                                          verify=verify,
                                                          files=files,
                                                          **kwargs)
                except Exception as e:
                    logger.error(f"request failed! cause by: {e}")
                    raise
        else:
            logger.error(f"不支持{method}方法")
            result = None
        return result

    def close(self):
        self.request_session.close()

    def __del__(self):
        self.request_session.close()


if __name__ == '__main__':
    aa = RequestHttp()
    url = "https://www.kgvod.com/vod-show-32-by-score-page-1.html"
    res = aa.send("get", url).text
    print(res)
